﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

namespace FeatureBuilder.Contrib
{
    /// <summary>
    /// Overrides of 3 methods which allow this launch point
    /// execute even when the feature is not yet instantiated
    /// </summary>
    public partial class FBContribLaunchPoint : VsLaunchPoint
    {
        [Import]
        IFeatureManager FeatureManger { get; set; }

        /// <summary>
        /// The VsLaunchPoint connects, by default, to a IFeatureCommand via
        /// a Command Binding.  However, CommandBindings are always associated
        /// with a Feature Extension instance and, since we don't have one
        /// yet, we must put our code right here in the Execute method
        /// </summary>
        /// <param name="feature"></param>
        public override void Execute(IFeatureExtension feature)
        {
            //
            // First, see if our instance is already activated and,
            // if so, just make it the active FX
            foreach (IFeatureExtension fx in FeatureManager.InstantiatedFeatures)
            {
                if (fx.InstanceName == "FeatureBuilder.Contrib")
                {
                    FeatureManager.ActiveFeature = fx;
                    return;
                }
            }

            //
            // Ok, it's not instantiated yet, create it
            //
            var newInstance = FeatureManager.Instantiate("FeatureBuilder.Contrib", "FeatureBuilder.Contrib");
            FeatureManager.ActiveFeature = newInstance;

        }

        /// <summary>
        /// For standalone FX, always return null
        /// </summary>
        /// <param name="command">ignored</param>
        /// <returns>Always returns null</returns>
        protected override IFeatureExtension OnQueryStatus(Microsoft.VisualStudio.Modeling.ExtensionEnablement.IMenuCommand command)
        {
            return null;
        }

        /// <summary>
        /// For standalone FX, always return false
        /// </summary>
        /// <param name="feature">N/A</param>
        /// <returns>Always returns false</returns>
        public override bool CanExecute(IFeatureExtension feature)
        {
            return false;
        }

        /// <summary>
        /// Since we have no FX instance (as yet), always return true
        /// in order to get the menu item to appear
        /// </summary>
        public override bool Visible
        {
            get
            {
                return true;
            }
        }
    }
}

